import java.util.*;

public class demo1 {
    public List<String> topKFrequent(String[] words, int k) {
        HashMap<String,Integer>map=new HashMap<>();
        for(String temp:words){
            map.put(temp,map.getOrDefault(temp,0)+1);
        }
        List<String>list=new ArrayList<>();
        for(Map.Entry<String,Integer>entry:map.entrySet()){
            list.add(entry.getKey());
        }
        //利用自定义list的排序原理
      Collections.sort(list, new Comparator<String>() {
          @Override
          public int compare(String o1, String o2) {
              return map.get(o1)==map.get(o2)?o1.compareTo(o2):map.get(o2)-map.get(o1);
          }
      });
        return list.subList(0,k);
    }
}
